(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('adminCtrl',adminCtrl);

    function adminCtrl(authenticationService, $state, Constants, authorizationManager, feedbacksManager, ModalService, $scope){

        $scope.$on('promisesChangedEvent', function (event, data) {
            if(data.isLoading){
                $('#adminLoading').modal('show');
            }else{
                $('#adminLoading').modal('hide');
            };
        });


        this.STATES = Constants.STATES;

        this.isActive = function(nav){
            return (nav === $state.current.name);
        };

        this.logout = function(){
            authenticationService.logout();
        };

        this.getUnauthorizedUsersCount = function(){
            return authorizationManager.getUnauthorizedUsersCount();
        };

        this.getFeedbackCount = function(){
            return feedbacksManager.getOpenFeedbacksCount();
        };

        this.getMessagesCount = function(){
            return feedbacksManager.getUnreadMeesagesCount();
        };

        this.writeFeedback = function(){
            ModalService.showModal({
                templateUrl: 'editFeedback',
                controller: 'writeFeedbackCtrl as wfc'
            })
                .then(function(modal){
                    modal.element.modal();
                    modal.close.then(function(result){

                        if(result.action === Constants.COMMANDS.ADD){
                            feedbacksManager.addFeedback(result.feedback);
                        }
                    });
                });
        };

    };

    angular.module('ytong.controllers').controller('writeFeedbackCtrl', writeFeedbackCtrl);

    function writeFeedbackCtrl(close){

        this.feedback = {};
        this.feedback.title = "";
        this.feedback.content = "";

        this.close = function(action) {
            var result = {};
            result.action = action;
            result.feedback = this.feedback;
            close(result, 500); // close, but give 500ms for bootstrap to animate
        };

    };

}(angular));